package com.smartx.app.model;

import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class NodeInfoModel {

    public String network;
    public List<String> capabilities;
    public String clientId;
    public String coinbase;
    public String latestBlockNumber;
    public String latestBlockHash;
    public int activePeers;
    public int pendingTransactions;

    public static NodeInfoModel getNodeInfo(String json){
        NodeInfoModel nodeInfoModel = new NodeInfoModel();
        nodeInfoModel.capabilities = new ArrayList<>();

        try{
            JSONObject jsonObject = new JSONObject(json);
            JSONObject resultObject = jsonObject.getJSONObject("result");
            JSONArray capJsonArray = resultObject.getJSONArray("capabilities");
            for(int i=0;i < capJsonArray.length();i++){
                String cap = capJsonArray.getString(0);
                nodeInfoModel.capabilities.add(cap);
            }

            nodeInfoModel.clientId = resultObject.getString("clientId");
            nodeInfoModel.coinbase = resultObject.getString("coinbase");
            nodeInfoModel.latestBlockNumber = resultObject.getString("latestBlockNumber");
            nodeInfoModel.latestBlockHash = resultObject.getString("latestBlockHash");
            nodeInfoModel.activePeers = resultObject.getInt("activePeers");
            nodeInfoModel.pendingTransactions = resultObject.getInt("pendingTransactions");
            return nodeInfoModel;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
